package com.ford.trade.jingcai;

import com.ford.trade.dto.QueryDTO;
import com.ford.trade.entity.JcGame;
import com.ford.trade.entity.JingCaiPeiLv;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ford
 * @date: 2022/4/25 11:33
 */
@Slf4j
@RestController
public class JingCaiController {

    @Resource
    private JcGame500Service jcGame500Service;
    @Resource
    private JingCaiPeilvService jingCaiPeilvService;
    public static final List<String> months = Lists.newArrayList("1", "3", "5", "7");

    @PostMapping("getJingCai")
    public void getBisai(@RequestBody QueryDTO queryDTO) {
        JcGameProcessor teamProcessor = new JcGameProcessor();
        teamProcessor.setJcGame500Service(jcGame500Service);
        teamProcessor.setUrl(queryDTO.getUrl());
        teamProcessor.setGetResult(queryDTO.getGetResult());
        List<String> urls = new ArrayList<>();
        if (StringUtils.isNotBlank(queryDTO.getDate())) {
            for (int i = 1; i < 32; i++) {
                urls.add(queryDTO.getUrl() + queryDTO.getDate() + "-" + StringUtils.leftPad(String.valueOf(i), 2, "0"));
            }
        } else {
            for (int i = 1; i < 13; i++) {
                for (int j = 1; j < 32; j++) {
                    urls.add(queryDTO.getUrl() + StringUtils.leftPad(String.valueOf(i), 2, "0") + "-" + StringUtils.leftPad(String.valueOf(j), 2, "0"));
                }
            }
        }
        String[] urls2 = new String[urls.size()];
        String[] strings = urls.toArray(urls2);
        Spider.create(teamProcessor)
                .addUrl(strings)
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(500)))
                .thread(100)
                .run();
    }

    @PostMapping("getJcPeilv")
    public void getPeilv(@RequestBody QueryDTO queryDTO) throws ParseException {
        //获取一天的比赛
        List<JcGame> games = new ArrayList<>();
        if ("all".equals(queryDTO.getType())) {
            games = jcGame500Service.findAll1();
        }else {
            games = jcGame500Service.findAllByDateBetten(queryDTO.getDate());
        }
        if (CollectionUtils.isEmpty(games)) {
            return;
        }
        List<String> urls = new ArrayList<>();
        List<String> urls2 = new ArrayList<>();
        if ("1".equals(queryDTO.getGetResult())) {
            for (JcGame game : games) {
                List<JingCaiPeiLv> peiLv2s = jingCaiPeilvService.findByIndexId(game.getIndexId());
                if (!CollectionUtils.isEmpty(peiLv2s)) {
                    for (JingCaiPeiLv peiLv2 : peiLv2s) {
                        peiLv2.setResult(game.getResult());
                        jingCaiPeilvService.save(peiLv2);
                    }
                }
            }
        } else {
            for (JcGame game : games) {
                List<JingCaiPeiLv> peiLv2s = jingCaiPeilvService.findByIndexId(game.getIndexId());
                if (CollectionUtils.isEmpty(peiLv2s)) {
                    urls.add("https://odds.500.com/fenxi/ouzhi-" + game.getIndexId() + ".shtml");
                    urls2.add("https://odds.500.com/fenxi/rangqiu-" + game.getIndexId() + ".shtml");
                }
            }
            String[] objects = new String[urls.size()];
            String[] objects2 = new String[urls.size()];
            String[] array = urls.toArray(objects);
            String[] array2 = urls2.toArray(objects2);
            JingCaiPeilvProcessor peilv2Processor = new JingCaiPeilvProcessor();
            peilv2Processor.setCookie(queryDTO.getCookie());
            peilv2Processor.setJingCaiPeilvService(jingCaiPeilvService);
            peilv2Processor.setDate(queryDTO.getDate());
            peilv2Processor.setGames(games);
            peilv2Processor.setType(queryDTO.getType());
            if (0==urls.size()){
                return;
            }
            Spider.create(peilv2Processor)
                    .addUrl(array)
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(10000)))
                    .thread(32)
                    .run();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Spider.create(peilv2Processor)
                    .addUrl(array2)
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(10000)))
                    .thread(32)
                    .run();
        }
    }
}
